package com.iwayin.web.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.iwayin.core.bo.user.UserBO;
import com.iwayin.core.common.Constants;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.utils.ImageUtil;
import com.iwayin.core.utils.UpYun;
import com.iwayin.core.utils.UtilCommon;

public class PictureUploadAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File uploadPic; // 和JSP中input标记name同名
	private HttpServletResponse response;
	private String uploadPicFileName; // 上传来的文件的名字
	private int x;
	private int y;
	private int w;
	private int h;
	private int height;
	private int width;
	private String imagePath;
	private UserBO userBO;

	public String uploadPic() {
		response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8"); // 务必，防止返回文件名是乱码
		String exendName = UtilCommon.buildExtendName(uploadPicFileName); // 获取文件扩展名
		String newFileName = UtilCommon.buildNewFileName(uploadPicFileName); // 获取保存文件名称
		PrintWriter out;
		try {
			out = response.getWriter();
			if (uploadPic.length() > 0l && (double) uploadPic.length() / (1024 * 1024) <= 5) {
				if (UtilCommon.checkIsImage(exendName)) {
					UpYun upyun = new UpYun(Constants.ASK_IMG_BUCKET, Constants.ASK_IMG_USERNAME, Constants.ASK_IMG_PASSWORD);
					upyun.writeFile("/" + newFileName, uploadPic);
					String imageUrl = Constants.ASK_IMG_DOMAIN + newFileName;
					out.print("success#" + imageUrl+"!small");
				} else {
					out.print("error");
				}
			} else {
				out.print(this.getText("upload.image.size"));
			}
		} catch (Exception e) {
			try {
				response.getWriter().print(this.getText("upload.image.failure"));
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public String updateImage() {
		try {
			String smallAvatar = "";
			response = ServletActionContext.getResponse();
			PrintWriter out = response.getWriter();
			String newFileName = UtilCommon.buildNewFileName(imagePath); // 获取保存文件名称
			String exendName = imagePath.substring(imagePath.lastIndexOf("."));
			String tempPath = ServletActionContext.getServletContext().getRealPath("/yoaskPic"); // 获取项目根路径
			File uploadDir = new File(tempPath);
			if (!uploadDir.exists()) {
				uploadDir.mkdirs();
			}
//			URL url = new URL(imagePath);
//			BufferedInputStream bis = new BufferedInputStream(url.openStream());
//			BufferedImage bi = ImageIO.read(bis);
			String targetPath = tempPath + "/" + newFileName;
			uploadPic = ImageUtil.changeImg(targetPath, imagePath, width, height, exendName);
			// String targetPath = tempPath + "/" + newFileName;
			File file = ImageUtil.abscut(uploadPic, x, y, w, h, targetPath, imagePath);
			File file2 = ImageUtil.changeImg2(targetPath, file, 265, 265, exendName);
			if (file != null) {
				UpYun upyun = new UpYun("yoasking-user-face", "yoasking", "xiaobo612");
				upyun.writeFile("/" + newFileName, file2);
				smallAvatar = "http://face.yoasking.com/" + newFileName;
				out.print("success#" + smallAvatar);
			}
			if (smallAvatar.equals("")) {
				smallAvatar = imagePath;
			}
			UserDO user = this.getLoginUser();
			user.setOriginalAvatarPath(imagePath);
			user.setAvatarPath(smallAvatar);
			userBO.updateUserInfo(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String uploadTouXiang() {
		response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8"); // 务必，防止返回文件名是乱码
		String exendName = UtilCommon.buildExtendName(uploadPicFileName); // 获取文件扩展名
		String newFileName = UtilCommon.buildNewFileName(uploadPicFileName); // 获取保存文件名称
		PrintWriter out;
		try {
			out = response.getWriter();
			if (uploadPic.length() > 0l && (double) uploadPic.length() / (1024 * 1024) <= 5) {
				if (UtilCommon.checkIsImage(exendName)) {
					// InputStream inputStream =new FileInputStream(uploadPic);
					// BufferedInputStream bis = new
					// BufferedInputStream(inputStream);
					// BufferedImage bi =ImageIO.read(bis);
					// int height=bi.getHeight();
					// if(bi.getWidth()>265){
					// String tempPath
					// =ServletActionContext.getServletContext().getRealPath("/yoaskPic");
					// // 获取项目根路径
					// File uploadDir = new File(tempPath);
					// if (!uploadDir.exists()) {
					// uploadDir.mkdirs();
					// }
					// String targetPath = tempPath + "/" + newFileName;
					// uploadPic=ImageUtil.changeImg2(targetPath, uploadPic,
					// 265, 265, exendName);
					// }
					UpYun upyun = new UpYun("yoasking-user-face", "yoasking", "xiaobo612");
					upyun.writeFile("/" + newFileName, uploadPic);
					String imageUrl = "http://face.yoasking.com/" + newFileName;
					out.print("success#" + imageUrl);
				} else {
					out.print("error");
				}
			} else {
				out.print(this.getText("upload.image.size"));
			}
		} catch (Exception e) {
			try {
				response.getWriter().print(this.getText("upload.image.failure"));
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public File getUploadPic() {
		return uploadPic;
	}

	public void setUploadPic(File uploadPic) {
		this.uploadPic = uploadPic;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getUploadPicFileName() {
		return uploadPicFileName;
	}

	public void setUploadPicFileName(String uploadPicFileName) {
		this.uploadPicFileName = uploadPicFileName;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = (int) x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getW() {
		return w;
	}

	public void setW(int w) {
		this.w = w;
	}

	public int getH() {
		return h;
	}

	public void setH(int h) {
		this.h = h;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	public void setUserBO(UserBO userBO) {
		this.userBO = userBO;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

}
